//
//  DeviceListTableViewCell.m
//  Rail
//
//  Created by hychen on 2018/7/22.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "DeviceListTableViewCell.h"
#import "BMCustomButton.h"

@implementation DeviceListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setArray:(NSArray *)array {
    _array = array;
    [self setupUI];
}

- (void)setupUI {
    [self.contentView removeAllSubviews];
    NSInteger itemW = 100;
    for (int i = 0; i < _array.count; ++i) {
        CGFloat leftPadding = (SCREEN_WIDTH-itemW*_array.count)*0.5 + i*itemW;
        NSDictionary *dict = [_array objectAtIndexSafe:i];
        BMCustomButton *button = [BMCustomButton buttonWithType:UIButtonTypeCustom];
        button.customStyle = CustomButtonStyleImageTopTitleBottom;
        button.imageWidth = 45;
        button.imageHeight = 45;
        [button setTitle:[dict objectForKeySafe:@"title"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [button setTitleColor:kBlackColor forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[dict objectForKeySafe:@"image"]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        [self.contentView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentView);
            make.width.mas_equalTo(itemW);
            make.left.mas_equalTo(leftPadding);
        }];
    }
}

- (void)clickButton:(UIButton *)button {
    if (self.BlockClickButton) {
        NSDictionary *dict = [_array objectAtIndexSafe:button.tag];
        self.BlockClickButton([dict objectForKeySafe:@"sign"]);
    }
}

@end
